//
//  CPSCCouponCell.m
//  CollTest
//
//  Created by codew on 10/22/21.
//

#import "CPSCCouponCell.h"

// 初始 TOP
#define KValueLblPriceInitTop 25

#define KValueLblLeftItemMargin 4
#define KValueLblLeftItemHeight 17
#define KValueLblPriceHeight 28

#define KValueItemMaxY (KValueLblPriceInitTop + (KValueLblLeftItemMargin + KValueLblLeftItemHeight)*2 +  KValueLblPriceHeight)


// 图片的宽度
#define KValueImageWidth 1053
// 图片的高度
#define KValueImageHegit 366


// 内容的宽度
#define KValueCellContentRightLeftMargin (10)
#define KValueCellContentWidth (ScreenWidth - KValueCellContentRightLeftMargin *2 )
#define KValueCellHeight ( KValueImageHegit * KValueCellContentWidth / KValueImageWidth )

#define KValueCellContentHeight ( KValueCellHeight * KValueHCRatio )

// 左边红色内容部分的宽高比
#define KValueCellContentHWRatio (1.06)


#define KValueHTopRatio (5*0.01)
#define KValueHCRatio (86.8*0.01)
#define KValueHBottomRatio (8.2*0.01)


#define KValueWLeftRatio (2.28*0.01)
#define KValueWCRatio (95.44*0.01)
#define KValueWRightRatio (2.28*0.01)

/**
 图片的高度366,
 HTop: 18   --------------->5%
 HC: 318    --------------->86.8%
 HBottom: 30--------------->8.2%
 
 图片的宽度: 1053
 WLeft: 24 -----------------------> 2.28 %
 WC: 1005  -----------------------> 95.44 %
 WRight: 24 -----------------------> 2.28 %
 */

@interface CPSCCouponCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageViewBG;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewLeft;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewSelectStatus;
/** 满减券/折扣券 */
@property (weak, nonatomic) IBOutlet UILabel *lblMark;
/** 优惠券名称 */
@property (weak, nonatomic) IBOutlet UILabel *lblName;
/** "xxx商品"可用 */
@property (weak, nonatomic) IBOutlet UILabel *lblSummary;
/** 有效期 */
@property (weak, nonatomic) IBOutlet UILabel *lblDate;
/** 价钱 */
@property (weak, nonatomic) IBOutlet UILabel *lblPrice;
/**  */
@property (weak, nonatomic) IBOutlet UILabel *lblLeftItem1;
/**  */
@property (weak, nonatomic) IBOutlet UILabel *lblLeftItem2;
/**  */
@property (weak, nonatomic) IBOutlet UIView *viewContentSuper;

@property (nonatomic, strong) UILabel * lblMarkContent;

// layout ====================================================================================


// 左边view

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewSuperLeft;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftViewSuperLeft;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthViewSuperLeft;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightViewSuperLeft;

// 右边
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewSuperRight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftViewSuperRight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthViewSuperRight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightViewSuperRight;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLblPirce;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLblName;

@end

@implementation CPSCCouponCell

+ (CGFloat)heightCell
{
    CGFloat height = KValueCellHeight;
    
    return height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    [self clearDebugColor];
    [self updateCellCPSCLayout];
    
        
    CAGradientLayer * layer = [self setGradualChangingColor:self.lblMark fromColor:@"#FA5741" toColor:@"#FA6D5A"];
    [self.lblMark.layer addSublayer:layer];
//    [self.viewContentSuper mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.bottom.mas_equalTo(0);
//    }];

    self.lblMark.layer.cornerRadius = 2;
    self.lblMark.layer.masksToBounds = YES;
    
    [self.lblMark addSubview:self.lblMarkContent];
    [self.lblMarkContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    self.lblMarkContent.text = @"满减券";

}

- (void)updateCellCPSCLayout
{
    self.topViewSuperLeft.constant = KValueCellHeight * KValueHTopRatio;
    self.leftViewSuperLeft.constant = KValueCellContentWidth * KValueWLeftRatio;
    self.heightViewSuperLeft.constant = KValueCellContentHeight;
    self.widthViewSuperLeft.constant = KValueCellContentHeight / KValueCellContentHWRatio;
    
    
    self.topViewSuperRight.constant = KValueCellHeight * KValueHTopRatio;
    self.leftViewSuperRight.constant = 0;
    self.widthViewSuperRight.constant = KValueCellContentWidth - (KValueCellContentHeight / KValueCellContentHWRatio) - KValueCellContentWidth *KValueWRightRatio *2;
    self.heightViewSuperRight.constant = KValueCellContentHeight;
    
    
    
    if (KValueItemMaxY > KValueCellContentHeight) {
        
        self.topLblPirce.constant = 10;
        self.topLblName.constant = 10;
    }
    
}


- (void)clearDebugColor
{
    
    NSArray *array = @[
        
        self.imageViewBG,
        self.imageViewLeft,
        self.imageViewSelectStatus,
        /** 满减券/折扣券 */
        self.lblMark,
        /** 优惠券名称 */
        self.lblName,
        /** "xxx商品"可用 */
        self.lblSummary,
        /** 有效期 */
        self.lblDate,
        /** 价钱 */
        self.lblPrice,
        /**  */
        self.lblLeftItem1,
        /**  */
        self.lblLeftItem2,
        /**  */
        self.viewContentSuper
    ];
    
    for (UIView *view in array) {
        
        view.backgroundColor = [UIColor clearColor];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)updateCellWithItem:(id)item showAddMore:(BOOL)showAddMore
{
    
}

#pragma mark - SetterGetter

- (UILabel *)lblMarkContent
{
    if (!_lblMarkContent) {
        
        UILabel * lblZy = [[UILabel alloc] init];
        lblZy.textColor = [UIColor whiteColor];
        lblZy.font = [UIFont systemFontOfSize:10];
        lblZy.textAlignment = NSTextAlignmentCenter;
        
        _lblMarkContent = lblZy;
    }
    
    return _lblMarkContent;
}



#pragma mark - privte


//绘制渐变色颜色的方法
- (CAGradientLayer *)setGradualChangingColor:(UIView *)view fromColor:(NSString *)fromHexColorStr toColor:(NSString *)toHexColorStr{

//    CAGradientLayer类对其绘制渐变背景颜色、填充层的形状(包括圆角)
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = view.bounds;
    
    //  创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:fromHexColorStr].CGColor,(__bridge id)[UIColor colorWithHexString:toHexColorStr].CGColor];

    //  设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);

    //  设置颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations = @[@0,@1];

    return gradientLayer;
}

@end
